package com.sojson.util.encrypt.bothway.imparity.sm2.bean;

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.math.ec.ECPoint;

import com.sojson.util.encrypt.bothway.imparity.bean.CustomKeyPair;
import com.sojson.util.encrypt.bothway.imparity.sm2.Sm2Util;

/**
 * Sm2非对称加解密Bean
 * 
 * @author liu
 * @date 2020-09-12
 */
public class Sm2KeyPair extends CustomKeyPair {

    /***/
    private static final long serialVersionUID = 305652430410636313L;

    /** 公钥 */
    private ECPoint publicKeyPoint;
    /** 私钥 */
    private BigInteger privateKeyInt;

    public Sm2KeyPair(PublicKey publicKey, PrivateKey privateKey) throws Exception {
        super(publicKey, privateKey);
    }

    public Sm2KeyPair(String publicKey, String privateKey) throws Exception {
        super(publicKey, privateKey);
    }

    public Sm2KeyPair(byte[] publicKey, byte[] privateKey) throws Exception {
        super(publicKey, privateKey);
    }

    public Sm2KeyPair(ECPoint publicKey, BigInteger privateKey) throws Exception {
        pointToByte(publicKey, privateKey);
        byteToStr(getPublicKeyByte(), getPrivateKeyByte());
    }

    /** 获取 公钥 */
    public ECPoint getPublicKeyPoint() {
        return publicKeyPoint;
    }

    /** 设置 公钥  */
    public void setPublicKeyPoint(ECPoint publicKeyPoint) {
        this.publicKeyPoint = publicKeyPoint;
    }

    /** 获取 私钥 */
    public BigInteger getPrivateKeyInt() {
        return privateKeyInt;
    }

    /** 设置 私钥  */
    public void setPrivateKeyInt(BigInteger privateKeyInt) {
        this.privateKeyInt = privateKeyInt;
    }
    @Override
    protected void byteToObj(byte[] publicKey, byte[] privateKey) throws Exception {
        final BouncyCastleProvider bc = new BouncyCastleProvider();
        KeyFactory keyFact = KeyFactory.getInstance("EC", bc);
        setPublicKey(keyFact.generatePublic(new X509EncodedKeySpec(publicKey)));
        setPrivateKey(keyFact.generatePrivate(new PKCS8EncodedKeySpec(privateKey)));
    }

    private void pointToByte(ECPoint publicKey, BigInteger privateKey) throws Exception {
        setPublicKeyPoint(publicKey);
        setPrivateKeyInt(privateKey);
        setPublicKeyByte(publicKey.getEncoded(false));
        setPrivateKeyByte(privateKey.toByteArray());
    }

    /**
     * 加密
     * 
     * @param data   内容
     * @param publicKey 公钥
     * @return
     * @throws Exception 
     */
    @Override
    protected String encryptContentToStr(byte[] data) {
        return Sm2Util.encryptContentToStr(data);
    }

    /**
     * 解密
     * 
     * @param data       内容
     * @param privateKey    私钥
     * @return
     * @throws Exception 
     */
    @Override
    protected byte[] decryptContentToByte(String data) {
        return Sm2Util.decryptContentToByte(data);
    }

}